<?php
	
	/** 
	 * encapsulate methods for the table 'o_lu_refs'
	 */
	class Ref_Ol extends Model_Abstract{
		
		public function add($o_id, $lu_id, $weight){
			try{
				$sql = 'insert into o_lu_refs set o_id=?, lu_id=?, weight=?';
				$result = $this->_connection->execute($sql, array($o_id, $lu_id, $weight));
				return $result;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function delete($id){
			try{
				$sql = 'delete from o_lu_refs where id=?';
				$result = $this->_connection->execute($sql, array($id));
				return $result;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		//delete the relation between the Offer and Landingurl by offer_id, landingurl_id
		public function deleteByOL($offer_id, $landingurl_id){
			try{
				$sql = 'delete from o_lu_refs where o_id=? and lu_id=?';
				$result = $this->_connection->execute($sql, array($offer_id, $landingurl_id));
				return $result;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function canDelete($o_id){
			try{
				$sql = 'select count(*) from o_lu_refs where o_id=?';
				$result = $this->_connection->execute($sql, array($o_id));
				if($result && $result>1)
					return true;
				return false;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function cloneRef($o_id, $new){
			try{
				$sql = 'select lu_id from o_lu_refs where o_id=?';
				$result = $this->_connection->query($sql, array($o_id));
				if($result){
					foreach ($result as $row){
						$success = $this->add($new, $row['lu_id'], $row['weight']);
						if(!$success)
							return false;
					}
				}
				return true;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function getRelatedLandings(){
			try{
				$sql = 'select lu.id as lu_id, url, ol.o_id, weight
						 from landing_urls as lu left join o_lu_refs as ol on lu.id=ol.lu_id';
				$result = $this->_connection->query($sql);
				return $result;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function changeRelatedLUToOffer($offer_id, $landingurls){
			try{
				$sql = 'select id, lu_id from o_lu_refs where o_id=?';
				$result = $this->_connection->query($sql, array($offer_id));
		
				if(count($result) > 0){
					//delete landing urls that no longer related to this offer
					$selectedLu = array();
					foreach ($result as $ol) {
						$selectedLu[$ol['id']] = $ol['lu_id'];
					}
					$postedIds = $this->getPostedLandingurlIds($landingurls);
					$unRelatedLandingurls = array_diff($selectedLu, $postedIds);
					if(count($unRelatedLandingurls) > 0){
						foreach ($unRelatedLandingurls as $key=>$v){
							$this->delete($key);
						}
					}
					
					foreach ($postedIds as $postedId){
						if(!$key = array_search($postedId, $selectedLu)){
							//add new landing urls to the offer
							$this->add($offer_id, $postedId, $this->getWeightById($landingurls, $postedId));
						}else{
							$postedWeight = $this->getWeightById($landingurls, $postedId);
							if($this->getSelectedLUWeight($result, $key) != $postedWeight){
								$this->updateWeightById($key, $postedWeight);
							}
						}
					}
				}else{
					foreach ($landingurls as $landingurl){
						if(isset($landingurl['id'])){
							$result = $this->add($offer_id, $landingurl['id'], $landingurl['weight']);
							if(!$result)
								return false;
						}
					}
					return  true;
				}
				
				
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		private function getSelectedLUWeight($selectedLUs, $id){
			foreach ($selectedLUs as $selectedLU){
				if($selectedLU['id'] == $id)
					return $selectedLU['weight'];
			}
		}	
		
		
		private function updateWeightById($id, $weight){
			try{
				$sql = 'update o_lu_refs set weight=? where id=?';
				$result = $this->_connection->execute($sql, array($weight, $id));
				return $result;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		private function getPostedLandingurlIds($landingurls){
			$ids = array();
			foreach ($landingurls as $landingurl){
				if(isset($landingurl['id'])){
					$ids[] = $landingurl['id'];
				}
			}
			return $ids;
		}
		
		private function getWeightById($landingurls, $id){
			foreach ($landingurls as $landingurl) {
				if($id == $landingurl['id'])
					return $landingurl['weight'];
			}
			return 1;
		}
		
	}